package com.daisuxia.risk.service;

import java.util.HashMap;
import java.util.LinkedHashMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.daisuxia.back.utils.JxlUtils;
import com.daisuxia.back.utils.ServiceResult;
import com.daisuxia.back.utils.SysCacheUtils;
import com.daisuxia.mg.model.UserGridResponse;
import com.daisuxia.mg.service.UserGridApi;
import com.daisuxia.risk.pojo.RiskOrders;
import com.daisuxia.risk.utils.ConstantRisk;
import com.daisuxia.web.pojo.BackConfigParams;
import com.daisuxia.web.util.JSONUtil;
import com.daisuxia.web.util.OrderNoUtil;
/**
 * 蜜罐
 * @author zhangsh Tel:13817876783
 * @date 2017年10月18日 上午9:18:32
 * @Title: JxlMgService
 * @ClassName: JxlMgService
 * @Description:
 */
@Service
public class JxlMgService implements IJxlMgService {
	Logger logger = LoggerFactory.getLogger(getClass());
	@Autowired
	private IRiskOrdersService riskOrdersService;

	@Override
	public ServiceResult getDetail(HashMap<String, Object> params) {
		ServiceResult serviceResult = new ServiceResult("500", "未知异常，请稍后重试！");
		try {
			Object userName = params.get("userName");
			Object cardNum = params.get("cardNum");
			Object userId = params.get("userId");
			Object userPhone = params.get("userPhone");
			if (userName != null && cardNum != null && userId != null
					&& userPhone != null) {
				RiskOrders orders = new RiskOrders();
				orders.setUserId(userId + "");
				orders.setOrderType(ConstantRisk.MG);
				orders.setAct(ConstantRisk.MG_BLACK);
				orders.setOrderNo(OrderNoUtil.getInstance().getUUID());
				orders.setReqParams("userId=" + userId + ";userName="
						+ userName + ";cardNum=" + cardNum + ";userPhone="
						+ userPhone);
				logger.info("mg card"+cardNum);
//				logger.info("getDetail send  " + orders.toString());
				riskOrdersService.insert(orders);
				LinkedHashMap<String, String> map2 = SysCacheUtils
						.getConfigParams(BackConfigParams.JXL);
//				UserGridResponse gridInfo = UserGridApi.invokeUserGridApi(map2
//						.get("JXL_token"), map2.get("JXL_BZM"), userName + "",
//						cardNum + "", userPhone + "");
				UserGridResponse gridInfo = UserGridApi.invokeUserGridApi(JxlUtils.getAccessReportToken(), map2.get("JXL_BZM"), userName + "",
						cardNum + "", userPhone + "");
//				logger.info("getDetail return " + gridInfo);
				if (gridInfo != null) {
					if (gridInfo.isSuccess()) {
						serviceResult = new ServiceResult(
								ServiceResult.SUCCESS, JSON.toJSONString(gridInfo));
						orders.setStatus(RiskOrders.STATUS_SUC);
					} else {
						serviceResult = new ServiceResult("300", gridInfo
								.getMessage());
					}
					orders.setReturnParams(JSONUtil.beanToJson(gridInfo));
					riskOrdersService.update(orders);
				} else {
					orders.setReturnParams("return null");
					serviceResult = new ServiceResult("100", "返回空或请求报错");
				}
				riskOrdersService.update(orders);
			} else {
				serviceResult = new ServiceResult("400", "必要参数不足！");
			}
		} catch (Exception e) {
			logger.error("getDetail error ,params=" + params, e);
		}
		return serviceResult;
	}

}
